﻿/**
 * 本质上就是一个二维偏序，对(xi, yi)，问比xi大的且比yi小的点有多少个
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>
using namespace __gnu_pbds;

struct FenwickTree{ // 树状数组

using value_type = long long int;
using vec_type = vector<value_type>;

int n;
vec_type c;

FenwickTree() = default;

static int lowbit(int x){return x & -x;}

void init(int nn){this->c.assign((this->n=nn) + 1, 0);}

void modify(int pos, value_type delta){
    for(int i=pos;i<=this->n;i+=lowbit(i)) this->c[i] += delta;
}

value_type query(int pos)const{
    value_type ans = 0;
    for(int i=pos;i;i-=lowbit(i)) ans += this->c[i];
    return ans;
}

value_type query(int s, int e)const{return this->query(e) - this->query(s - 1);}

}Bt;

using llt = long long;
using pii = pair<int, int>;


int N;
vector<pii> A;
vector<int> W;

llt proc(){
    W.clear(); W.reserve(N + N + 1); W.push_back(-2E9);
    for(const auto & p : A) W.push_back(p.first), W.push_back(p.second);
    sort(W.begin(), W.end());
    
    Bt.init(N + N);
    sort(A.begin(), A.end(), greater<pii>());
    llt ans = 0;
    for(const auto & p : A){
        auto pos = lower_bound(W.begin(), W.end(), p.second) - W.begin();
        ans += Bt.query(pos);
        Bt.modify(pos, 1);
    }
    return ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
    int nofkase = 1;
    const array<string, 3> t {"ABC", "CAB", "BCA"};
    cin >> nofkase;
    while(nofkase--){
        cin >> N;
        A.assign(N, {});
        for(auto & p : A) cin >> p.first >> p.second;
        cout << proc() << "\n";
    }
    return 0;
}